<?php
class AdminAction extends Action{
	
	public function _initialize()
	{
		//初始化操作
		if (!D('Admin')->islogin())
		{
			redirect(U('public/adminLogin'));
		}
	}
	
	public function index()
	{
		
		$this->display();
	}
	/**
    +----------------------------------------------------------
    * 后台头部页面
    +----------------------------------------------------------
    */
	public function top()
	{
		$menu = require (APP_PATH.'/Conf/adminMenu.cfg.php');
		$this->assign('username',$_SESSION['YunPHPName']);
		$this->assign('menu',$menu);
		$this->display();
	}
		
	/**
    +----------------------------------------------------------
    * 后台左侧页面
    +----------------------------------------------------------
    */
	public function left()
	{
		$menus = require (APP_PATH.'/Conf/adminMenu.cfg.php');
		if (!empty($_GET['tab'])){
			$menus = $menus[$_GET['tab']]['sub'];	
		}else{
			$menus = array_shift($menus);
			$menus = $menus['sub'];
		}
		$this->assign('menus',$menus);
		$this->display();
		
	}
	
	/**
    +----------------------------------------------------------
    * 后台body页面
    +----------------------------------------------------------
    */
	function body()
	{
		$serverinfo =array();
		$serverinfo['php_version'] = PHP_OS.' / PHP v'.PHP_VERSION;//PHP版本
		$serverinfo['server_software'] = $_SERVER['SERVER_SOFTWARE'];//服务器版本
		$mysqlinfo = M('')->query("SELECT VERSION() as version");
		$serverinfo['mysqlinfo'] = $mysqlinfo[0]['version'] ;//Mysql 版本
		$serverinfo['max_upload'] = ( @ini_get('file_uploads') )? ini_get('upload_max_filesize') : '<font color="red">获取失败</font>';//最大上传大小
		$t = M('')->query("SHOW TABLE STATUS LIKE '".C('DB_PREFIX')."%'");
        foreach ($t as $k){
            $dbsize += $k['Data_length'] + $k['Index_length'];
        }
        $serverinfo['db_size'] = sizecount($dbsize);//数据库大小
		$this->assign('serverinfo',$serverinfo);
		$this->display();
	}
	
	//评论管理
	
	public function commentList()
	{
		import("ORG.Util.Page");
		$comment = M('comment');
		$count = $comment->count();
		if ($count)
		{
			$Page = new Page($count,25); 
			$show = $Page->show();
			$commentData = $comment->order('comment_approved asc,comment_ID desc')->limit($Page->firstRow.','.$Page->listRows)->field('comment_ID,comment_author,comment_author_email,comment_author_ip,comment_approved,comment_content,comment_type')->select();
			$this->assign('commentData',$commentData);
			$this->assign('page',$show);
			
		}
		else
		{
			$this->assign('errorMsg','暂无评论信息');
		}
		$this->display();
		
		
		
	}
	
	//评论删除
	public function commentDel()
	{
		$comment_ID = $_GET['comment_ID'] ? intval($_GET['comment_ID']) : '0';
		if ($comment_ID != '0')
		{
			if (M('comment')->where(array('comment_ID'=>$comment_ID))->delete())
			{
				$this->success('删除成功!');
			}
		}
		$this->error('删除失败!');
	}
	
	
	//审核评论
	public function commentStatus()
	{
		$comment_ID = $_GET['comment_ID'] ? intval($_GET['comment_ID']) : '0';
		if ($comment_ID != '0')
		{
			if (M('comment')->where(array('comment_ID'=>$comment_ID))->data(array('comment_approved'=>'1'))->save())
			{
				$this->success('审核成功!');
			}
		}
		$this->error('审核失败!');
	}
	/**
    +----------------------------------------------------------
    * 分类列表
    +----------------------------------------------------------
    */
	public function categoryList()
	{
		$catelist = M('category')->order('id desc')->findAll();
		$this->assign('catelist',$catelist);
		$this->display();
	}
	
	public function categoryAdd()
	{
		
		$this->display();
	}
	
	public function categoryDel()
	{
		$id = intval($_GET['id']);
		if (empty($id))$this->error('参数错误!');
		if (M('category')->where(array('id'=>$id))->delete())
		{
			D('category')->updateCategoryCache();//更新栏目缓存;
			$this->success('删除分类成功!');
		}
		else
		{
			$this->error('删除分类失败');
		}
	}
	
	public function categoryEdit()
	{
		$id = intval($_GET['id']);
		if (empty($id))$this->error('参数错误!');
		if ($data = M('category')->where(array('id'=>$id))->find())
		{
			$this->assign('categorySingle',$data);
			$this->display();
		}
		else
		{
			$this->error('无此分类');
		}
	}
	
	//文章列表
	public function articleList()
	{
		import("ORG.Util.Page");
		$posts = M('posts');
		$count = $posts->count();
		$Page = new Page($count,25); 
		$show = $Page->show();
		$dataList = $posts->join(C('DB_PREFIX').'category on '.C('DB_PREFIX').'posts.cid = '.C('DB_PREFIX').'category.id')->field('yp_posts.id,yp_posts.subject,yp_posts.is_top,yp_posts.createdate,yp_category.c_name,yp_posts.click_count')->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
//		dump($dataList);
		$this->assign('dataList',$dataList);
		$this->assign('page',$show);
		$this->display();
		
	}
	
	public function articleAdd()
	{
		$categoryData = getCategoryCache();
		$this->assign('categoryData',$categoryData);
		$this->display();
	}
	
	public function articleEdit(){
		$id = intval($_GET['id']);
		if (empty($id))
			$this->error('参数错误!');
		$result = M('posts')->where(array('id'=>$id))->find();
		if (!$result)
		{
			$this->error('此文章不存在');
		}
		else
		{
			$categoryData = getCategoryCache();
			$tags = D('tag')->getTag($id);
			foreach ($tags as $v)
			{
				$tag[] =$v['tagname'];	
			}
			$result['tag'] = implode($tag, ',');//tag读取
			
			
			$this->assign('categoryData',$categoryData);
			$this->assign('result',$result);
			$this->display();
		}
	}
	
	public function articleDel(){
		$id = intval($_GET['id']);
		if (empty($id))$this->error('参数错误!');
		if (M('posts')->where(array('id'=>$id))->delete())
		{
			$this->success('删除成功!');
		}
		else
		{
			$this->error('删除失败!');
		}
	}
	
	public function setting()
	{
		$site_config = D('setting')->getConfig();
		$this->assign('set_config',$site_config);
		$this->display();
	}
	
	//修改管理员密码
	public function editadminpwd()
	{
		$this->display();
	}
	
	//更新系统缓存
	public function updateCache()
	{
		import("ORG.Io.Dir");
		$dir = new Dir(APP_PATH.'/Runtime');
		$dir->del(APP_PATH.'/Runtime/Cache');
		echo "模板缓存清理成功!<br>";
		$dir->del(APP_PATH.'/Runtime/Data');
		echo "数据缓存清理成功!<br>";
		$dir->del(APP_PATH.'/Runtime');
		echo "项目缓存清理成功!<br>";
	}
	/**
    +----------------------------------------------------------
    * 图片上传
    +----------------------------------------------------------
    */
	public function upload()
	{
		require_once (APP_PATH.'/Common/JSON.php');
		$php_url = __ROOT__  . '/';
		//文件保存目录路径
		$save_path = APP_PATH . '/Uploads/archives/';
		//文件保存目录URL
		$save_url = $php_url . 'Uploads/archives/';
		//定义允许上传的文件扩展名
		$ext_arr = array('gif', 'jpg', 'jpeg', 'png', 'bmp');
		//最大文件大小
		$max_size = 1000000;
		//有上传文件时
		if (empty($_FILES) === false) {
			//原文件名
			$file_name = $_FILES['imgFile']['name'];
			//服务器上临时文件名
			$tmp_name = $_FILES['imgFile']['tmp_name'];
			//文件大小
			$file_size = $_FILES['imgFile']['size'];
			//检查文件名
			if (!$file_name) {
				alert("请选择文件。");
			}
			//检查目录
			if (@is_dir($save_path) === false) {
				alert("上传目录不存在。");
			}
			//检查目录写权限
			if (@is_writable($save_path) === false) {
				alert("上传目录没有写权限。");
			}
			//检查是否已上传
			if (@is_uploaded_file($tmp_name) === false) {
				alert("临时文件可能不是上传文件。");
			}
			//检查文件大小
			if ($file_size > $max_size) {
				alert("上传文件大小超过限制。");
			}
			//获得文件扩展名
			$temp_arr = explode(".", $file_name);
			$file_ext = array_pop($temp_arr);
			$file_ext = trim($file_ext);
			$file_ext = strtolower($file_ext);
			//检查扩展名
			if (in_array($file_ext, $ext_arr) === false) {
				alert("上传文件扩展名是不允许的扩展名。");
			}
			//创建文件夹
			$ymd = date("Ym");
			$save_path .= $ymd . "/";
			$save_url .= $ymd . "/";
			if (!file_exists($save_path)) {
				mkdir($save_path);
			}
			//新文件名
			$new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $file_ext;
			//移动文件
			$file_path = $save_path . $new_file_name;
			if (move_uploaded_file($tmp_name, $file_path) === false) {
				alert("上传文件失败。");
			}
			@chmod($file_path, 0644);
			$file_url = $save_url . $new_file_name;
			
			header('Content-type: text/html; charset=UTF-8');
			$json = new Services_JSON();
			echo $json->encode(array('error' => 0, 'url' => $file_url));
			exit;
		}
	}
}
?>